package cn.zcscloud.mall.controller;

import cn.zcscloud.mall.common.PageRequest;
import cn.zcscloud.mall.common.R;
import cn.zcscloud.mall.mbg.model.SmsFlashPromotion;
import cn.zcscloud.mall.service.SmsFlashPromotionService;
import cn.zcscloud.mall.utils.SpecialDateEditor;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@CrossOrigin
@Slf4j
@RestController
@RequestMapping("/flash")
public class SmsFlashPromotionController {
    @Autowired
    private SmsFlashPromotionService smsFlashPromotionService;


    @GetMapping("/list")
    public R getList(String keyword, Long pageNum, Long pageSize) {
        log.info("keyword = {},pageSize = {},pageNum = {}", keyword, pageSize, pageNum);
        Page<SmsFlashPromotion> page = new Page<SmsFlashPromotion>(pageNum, pageSize);
        LambdaQueryWrapper<SmsFlashPromotion> lqw = new LambdaQueryWrapper<>();
        lqw.like(keyword != null, SmsFlashPromotion::getTitle, keyword);
        IPage<SmsFlashPromotion> iPage = smsFlashPromotionService.page(page, lqw);
        return R.success(PageRequest.init(iPage));
    }

    /**
     * 添加活动
     *
     * @param
     * @return
     */
    @PostMapping("/create")
    //public R creaste(@RequestBody Map<String, String> map) {
    public R creaste(@RequestBody SmsFlashPromotion smsFlashPromotion) {
        /*SmsFlashPromotion smsFlashPromotion = new SmsFlashPromotion();
        smsFlashPromotion.init(map);*/
        log.info("smsFlashPromotion=={}", smsFlashPromotion);
        if (smsFlashPromotion.getTitle() == null) {
            return R.error("活动主题为null", 4002);
        }
        LambdaQueryWrapper<SmsFlashPromotion> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SmsFlashPromotion::getTitle, smsFlashPromotion.getTitle());
        List<SmsFlashPromotion> list = smsFlashPromotionService.list(lqw);
        if (list.isEmpty()) {
            smsFlashPromotionService.save(smsFlashPromotion);
            return R.success("");
        } else {
            return R.error(smsFlashPromotion.getTitle() + "活动已存在", 4001);
        }
    }

    @PostMapping("/delete/{id}")
    public R delect(@PathVariable Long id) {
        SmsFlashPromotion smsFlashPromotion = smsFlashPromotionService.getById(id);
        if (smsFlashPromotion.getStatus() == 1) {
            return R.error("请先下线该活动", 403);
        }
        //移除名下的商品

        //移除活动
        boolean b = smsFlashPromotionService.removeById(id);
        if (b) {
            return R.success("");
        }
        return R.error("移除失败", 403);
    }

    @PostMapping("/update/{id}")
    public R update(@RequestBody Map map) {
        SmsFlashPromotion smsFlashPromotion = new SmsFlashPromotion();
        smsFlashPromotion.init(map);
        LambdaQueryWrapper<SmsFlashPromotion> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SmsFlashPromotion::getTitle, smsFlashPromotion.getTitle())
                .ne(SmsFlashPromotion::getId,smsFlashPromotion.getId());
        List<SmsFlashPromotion> list = smsFlashPromotionService.list(lqw);

        if (!list.isEmpty()) {
            return R.error("活动标题冲突", 403);
        }
        smsFlashPromotionService.updateById(smsFlashPromotion);
        return R.success("");
    }

    @PostMapping("/update/status/{id}")
    public R updateStatus(Integer status,@PathVariable Long id){
        SmsFlashPromotion smsFlashPromotion = new SmsFlashPromotion();
        smsFlashPromotion.setStatus(status);
        smsFlashPromotion.setId(id);
        smsFlashPromotionService.updateById(smsFlashPromotion);
        return R.success("");
    }

}
